package com.nebula.maven.springbootmebula.nebula;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class NebulaService {
    private final NebulaTemplate nebulaTemplate;

    // 新增顶点（示例使用user标签）
    public NebulaResult addUser(Long userId, String name, Integer age) {
        String cypher = String.format(
                "INSERT VERTEX user(name, age) VALUES %d:('%s', %d)",
                userId, name, age
        );
        return nebulaTemplate.executeObject(cypher);
    }

    // 更新顶点属性
    public NebulaResult updateUser(Long userId, String name, Integer age) {
        String cypher = String.format(
                "UPDATE VERTEX %d SET user.name = '%s', user.age = %d",
                userId, name, age
        );
        return nebulaTemplate.executeObject(cypher);
    }

    // 查询顶点
    public NebulaResult getUser(Long userId) {
        String cypher = String.format("FETCH PROP ON user %d YIELD properties(vertex)", userId);
        return nebulaTemplate.executeObject(cypher);
    }

    // 删除顶点
    public NebulaResult deleteUser(Long userId) {
        String cypher = String.format("DELETE VERTEX %d", userId);
        return nebulaTemplate.executeObject(cypher);
    }
}

